package com.ler.yrmall.basic;

import java.io.Serializable;

/**
 * @author Leron
 * @version 1.0
 */
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer code;

    private String message;

    private boolean success;

    private T data;

    public Result() {
        this.code = 200;
        this.success = true;
    }

    public static Result<?> error(Integer code, String message) {
        Result<?> result = new Result<>();
        result.success = false;
        result.message = message;
        result.code = 500;
        return result;
    }

    public static Result<?> error() {
        return error(500, "未知异常，请联系管理员");
    }

    public static Result<?> error(String message) {
        return error(500, message);
    }

    public static <E> Result<E> ok(String message) {
        Result<E> result = new Result<>();
        result.code = 200;
        result.message = message;
        result.success = true;
        return result;
    }

    public static <T> Result<T> ok(T data) {
        Result<T> result = new Result<>();
        result.code = 200;
        result.success = true;
        result.data = data;
        return result;
    }

    public static <T> Result<T> ok() {
        Result<T> result = new Result<>();
        result.code = 200;
        result.success = true;
        return result;
    }

    public static <T> Result<?> unauthorized(String message) {
        Result<T> result = new Result<>();
        result.code = 401;
        result.setMessage(message);
        result.success = false;
        return result;
    }

    public static <T> Result<?> forbidden(String message) {
        Result<T> result = new Result<>();
        result.code = 403;
        result.setMessage(message);
        result.success = false;
        return result;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
